package com.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jack01
 */
@SpringBootApplication
@EnableCircuitBreaker
@RestController
@EnableHystrixDashboard
@RequestMapping("/*")
public class Service2Entry {

    private static final Logger LOGGER = LoggerFactory.getLogger(Service2Entry.class);

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Service2Entry.class, args);
    }

    @RequestMapping("/service")
    @HystrixCommand(fallbackMethod = "service2Error", commandProperties = {@HystrixProperty(name = HystrixPropertiesManager
            .CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "20"),
            @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "20"), @HystrixProperty
            (name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "1000")})
    public String service2() throws InterruptedException {
        LOGGER.info("sleep start!");
      /*  if (true) {
           throw new RuntimeException("Test2");
        }*/
        Thread.sleep(10000);
        LOGGER.info("sleep end!");
        return "test-service2";
    }

    public String service2Error() {
        return "service2Error-Exception";
    }
}
